//
//  MainViewController.m
//  225视听百科
//
//  Created by 张元阳(10) on 14-12-27.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MainViewController.h"
#import "HomeTableViewCell.h"
#import "ConnectModel.h"
#import "HomeModel.h"
#import "SecondHomeViewController.h"
#import "SettingViewController.h"
#import "UIImageView+WebCache.h"
@interface MainViewController ()

@end

@implementation MainViewController

- (void)dealloc
{
    [_tableView release];
    [super dealloc];
}


-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        self.num = 8;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"225视听百科";
    [self createView];
}



-(void)createView
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nabar.jpg"] forBarMetrics:UIBarMetricsDefault];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(330, 0, 25, 25);
    [rightButton setBackgroundImage:[UIImage imageNamed:@"setting.png"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(settingAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
   
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    
    [_tableView registerClass:[HomeTableViewCell class] forCellReuseIdentifier:@"homeCell"];
    
    _tableView.rowHeight = 100;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.pullDelegate = self;
    _tableView.canPullDown = YES;
    _tableView.canPullUp = YES;
    [self.view addSubview:_tableView];
    
    [self StartConnect];
    
    [_tableView release];
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"homeCell"];
    
    HomeModel *model = [_tableArray objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor blackColor];
    
    cell.nameLabel.text = model.nickname;
    cell.infoLabel.text = model.personDescribe;
    cell.numLabel.text = [NSString stringWithFormat:@"专辑数:%ld", (long)model.albums];
    
    [cell.homeImg setImageWithURL:[NSURL URLWithString:model.mediumLogo]];
    
    return cell;
}


-(void)settingAction:(id)sender
{
    SettingViewController *setting = [[SettingViewController alloc] init];
    
    [self.navigationController pushViewController:setting animated:YES];
    
    [setting release];
}



-(void)StartConnect
{
    [ConnectModel connectModelWithUrlStr:@"http://app.9nali.com/index/777?page_id=1&device=iPhone&version=1.1.5" parmaters:nil finished:^(NSData *data) {
       
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [bigDic objectForKey:@"list"];
        
        for (NSDictionary *dic in array) {
            HomeModel *model = [[HomeModel alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            [self.tableArray addObject:model];
            [model release];
        }
        
        [_tableView reloadData];
        
        
    }];
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondHomeViewController *second = [[SecondHomeViewController alloc] init];
    
    second.homeModel = [_tableArray objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:second animated:YES];
    
    [second release];
}



-(void)scrollView:(UIScrollView *)scrollView loadWithState:(LoadState)state
{
    if (state == PullDownLoadState) {
        [self performSelector:@selector(downLoadEnd) withObject:self afterDelay:2];
        
    }else {
        [self performSelector:@selector(upLoadEnd) withObject:self afterDelay:2];
    }
}


-(void)downLoadEnd
{
    _num = 15;
    [_tableView reloadData];
    _tableView.canPullDown = YES;
    [_tableView reloadData];
    [_tableView stopLoadWithState:PullDownLoadState];
}

-(void)upLoadEnd
{
    _num += 10;
    [_tableView reloadData];
    if (_num > 50) {
        _tableView.canPullUp = NO;
    }
    [_tableView stopLoadWithState:PullUpLoadState];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
